﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;

namespace TV4Home.Client.WMC.TVClient.UI
{
    public class Setup : PageBase
    {
        #region Private Fields
        #endregion

        #region Constructor
        public Setup()
        {
        }
        #endregion

        #region Properties
        #region UI
        private string inputEditBox;
        public string InputEditBox
        {
            get
            {
                return this.inputEditBox;
            }
            set
            {
                this.inputEditBox = value;
                FirePropertyChanged("InputEditBox");
            }
        }

        private bool inputEditBoxSubmitted = false;
        public bool InputEditBoxSubmitted
        {
            get
            {
                return this.inputEditBoxSubmitted;
            }
            set
            {
                this.inputEditBoxSubmitted = value;
                FirePropertyChanged("InputEditBoxSubmitted");
            }
        }

        private bool editBoxKeyInteractive = true;
        public bool EditBoxKeyInteractive
        {
            get
            {
                return this.editBoxKeyInteractive;
            }
            set
            {
                this.editBoxKeyInteractive = value;
                FirePropertyChanged("EditBoxKeyInteractive");
            }
        }

       
        #endregion
        #endregion

        #region Public Methods
        public bool CheckServerNetworkConnectivity(string serverName)
        {
            // add a slight delay, so the user sees the progress bar animation 
            System.Threading.Thread.Sleep(2000);

            // if the user entered a port -> just get the hostname
            if (serverName.Contains(':'))
                serverName = serverName.Split(new char[] { ':' })[0];

            // send 5 pings with a timeout of 500ms per ping
            return PingHelper.PingTarget(serverName, 5, 500);
        }

        public bool CheckServerTVEWebServiceConnectivity(string serverName)
        {
            // add a slight delay, so the user sees the progress bar animation 
            System.Threading.Thread.Sleep(2000);

            try
            {
                TVWebSvc.TVWebService webSvc = MP.GetTVEWebService(ApplicationSettings.Instance.TVWebServiceURL, serverName);

                if (!webSvc.Ping())
                    return false;

                if (!webSvc.TestConnectionToTVService())
                    return false;

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public void SetServerHostName(string serverName)
        {
            ApplicationSettings.Instance.ServerHostName = serverName;
            ApplicationSettings.Instance.IsConfigured = true;
            ApplicationSettings.Instance.Save();
        }
        #endregion

        #region Private Methods
        #endregion
    }
}
